package com.bdqn.redsi;

import com.bdqn.redsi.pojo.User;
import com.bdqn.redsi.service.UserService;
import jakarta.annotation.Resource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class JedisTester {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserService userService;
    @Resource
    private Jedis jedis;
    @Before
    public void setup(){
        jedis=new Jedis("127.0.0.1",6379);
        jedis.auth("yzf");
    }
    @Test
    public void testString() {
        //添加数据
        jedis.set("name", "ktjiaoyu");
        System.out.println(jedis.get("name"));
        jedis.append("name", "czkt");
        System.out.println(jedis.get("name"));
        jedis.del("name");
        System.out.println(jedis.get("name"));
        jedis.mset("name", "ktjiaoyu", "age", "17", "add", "cz");
        jedis.incr("age");
        System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("add"));
    }

    @Test
    public void stringRedisTemplateTest(){
        stringRedisTemplate.opsForValue().set("test","100",60*10, TimeUnit.SECONDS);
        //val做-1操作
        stringRedisTemplate.boundValueOps("test").increment(-1);
        //根据key获取缓存中的val
        String test = stringRedisTemplate.opsForValue().get("test");
        //val做+1操作
        stringRedisTemplate.boundValueOps("test").increment(1);
        //根据key获取过期时间
        Long expire = stringRedisTemplate.getExpire("test", TimeUnit.SECONDS);
        //根据key删除缓存
        stringRedisTemplate.delete("test");
        //判断key是否存在返回boolean值
        stringRedisTemplate.hasKey("546545");
        //向指定key中存放set集合
        stringRedisTemplate.opsForSet().add("red_123","1","2","3");
        //设置过期时间
        stringRedisTemplate.expire("red_123",1000, TimeUnit.MILLISECONDS);
        //根据key查看集合中是否存在指定数据
        stringRedisTemplate.opsForSet().isMember("red_123","1");
        //根据key获取set集合
        stringRedisTemplate.opsForSet().members("red_123");
    }
    public void redisTemplateTest(){
        //操作字符串
        redisTemplate.opsForCluster();
        //操作hash
        redisTemplate.opsForHash();
        //操作list
        redisTemplate.opsForList();
        //操作set
        redisTemplate.opsForSet();
        //操作zset
        redisTemplate.opsForZSet();
    }
    @Test
    public void test(){
        stringRedisTemplate.opsForValue().set("name","czkt");
        Assert.assertEquals("czkt",stringRedisTemplate.opsForValue().get("name"));
    }
    @Test
    public void testObj() throws InterruptedException {
       User user=new User("CZKT","18",1L,1);
        ValueOperations<String,User>operations=redisTemplate.opsForValue();
        operations.set("expire",user,1000,TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean expire = redisTemplate.hasKey("expire");
        if (expire){
                System.out.println("expire is true");
        }else {
            System.out.println("expire is false");
        }
    }
    @Test
    public void testHash(){
        HashOperations<String, Object,Object>hash=redisTemplate.opsForHash();
        hash.put("user","name","czkt");
        String value= (String) hash.get("user","name");
        System.out.println("hash name value"+value);
    }
    @Test
    public void testList(){
        ListOperations<String,String> list=redisTemplate.opsForList();
        list.leftPush("list1","accp");
        list.leftPush("list1","bdqn");
        list.leftPush("list1","czkt");
        String value = list.leftPop("list1");
        System.out.println("list1 value"+value.toString());

    }
    @Test
    public void testGetUser(){
        User user = userService.getUser(1L);
        System.out.println(user.toString());
    }
}
